package com.education.mymediaplayer.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.education.mymediaplayer.R;

/**
 * Created by zhonghang on 16/6/23.
 * 用于播放器的控制器和底层的surfaceview进行分离
 */

public class TestControlView extends FrameLayout {
    private TestVideoView mVideoView;

    public TestVideoView getVideoView() {
        return mVideoView;
    }

    public void setMediaPlayer(MediaPlayer mediaPlayer) {
        this.mediaPlayer = mediaPlayer;
    }

    private MediaPlayer mediaPlayer;
    private GestureDetector mGestureDetector;
    private GestureDetector.OnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (distanceX < 20 && distanceY < 20) {
                return false;
            }
            if (distanceX > distanceY) {
                Log.d("-----------", "快进移动的距离X轴" + distanceX + "   Y轴" + distanceY);
                //左右滑动,进行快进快退
            } else {
                //更改音量活着亮度
                Log.d("-----------", "音量移动的距离X轴" + distanceX + "   Y轴" + distanceY);
            }
            return true;
        }
    };

    public TestControlView(Context context) {
        super(context);
        init();
    }

    public TestControlView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TestControlView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TestControlView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        mGestureDetector = new GestureDetector(getContext(), gestureListener);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.video_control, null);
        addView(view, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        mVideoView = (TestVideoView) findViewById(R.id.video_test);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //用手势响应事件
        return mGestureDetector.onTouchEvent(event);
    }
}
